#include "../include/video.h"
#include "../include/defs.h"
#include "../include/kernel.h"
#include "../include/proc.h"
#include "../include/sched.h"
#include "../include/kasm.h"
#include "../include/io.h"

extern int last100[100];
extern PROCESS procesos[MAX_PROC];

extern int stdin;
extern int CurrentPID;
extern int CurrentTTY;
extern int page_pid[];

extern unsigned long *page_directory;
extern unsigned long *page_table2;

static int factorialOK(int x);
static int factorialNotOK(int x);

void Sleep(int n)
{
    int * pcurrPID;
    pcurrPID = wMalloc(sizeof(int));
    PROCESS *proc;
    _Cli();
        getVal("CurrentPID", (void*) pcurrPID);
        proc=GetProcessByPID(* pcurrPID);
        proc->sleep=n*-20;
    _Sti();
    _int8();
}

int
top(int argc, char **argv)
{
    PROCESS p, *proc;
    int i;
    int * key;
    int proce[MAX_PROC];
    char * estados[3] = {"Corriendo", "Esperando", "Listo"};
    int state;
    int * pcurrPID;

    key = wMalloc(sizeof(int));
    *key = -1;
    pcurrPID = wMalloc(sizeof(int));
    getVal("CurrentPID", (void*) pcurrPID);

    proc=GetProcessByPID(* pcurrPID);


    while(*key != 113)
    {
        if(CurrentTTY == proc->tty)
            read(stdin, (void *)key, 1);

        _Cli();
        clearScreen();

        printf("Top: Lista de procesos\n");
        printf("PID       CPU%        TTY       NOMBRE       ESTADO\n");

        for( i = 0 ; i < MAX_PROC ; i++ )
            proce[i] = 0;

        for(i=0;i<100;i++)
        {
            proce[last100[i]]++;
        }

        for( i = 0 ; i < MAX_PROC ; i++ )
        {
            p = procesos[i];

            if (p.free)
                continue;

            if(p.blocked || p.sleep < 0)
                state = 1;
            else if(p.pid == (* pcurrPID) )
                state = 0;
            else if( proce[p.pid] > 0)
                state = 2;
            printf("%d         %d         %d         %s         %s\n", p.pid, proce[p.pid]%101,
                                                                        p.tty ,p.name, estados[state]);
        }
//        _Sti();


        Sleep(1);
    }

    return;
}

int
nice(int pid, int priority)
{
    int i;

    for (i = 0; i < MAX_PROC; i++)
    {
        if(procesos[i].pid == pid)
        {
            procesos[i].priority = priority;
            return pid;
        }
    }
    return -1;
}


int factorialOK(int x)
{
    _Sti();
    char auxi[500];
    int h;
    printf("pid:");
    for(h = 0; h< 50; h++)
    {
        printf(" %d,", page_pid[h]);
    }
    printf("\n");
    Sleep(1);

    if (x == 0)
        return 1;
    else
        return (x * factorialOK(x-1));
}

int factorialNotOK(int x)
{
    _Sti();
    char auxi[4000];
    int h;

    printf("pid:");

    Sleep(1);


/*    for(h = 0; h< 50; h++)
    {
        printf(" %d,", page_pid[h]);
    }
    printf("\n");*/

    if (x == 0)
        return 1;
    else
        return (x * factorialNotOK(x-1));
}

int factOK(int argc, char* argv[])
{
    int x = (int) argv;
    int ret = 0;
    _Sti();

    ret = factorialOK(x);
    printf("The factorial of 10 is: %d\n", ret);

    return 1;
}

int factNotOK(int argc, char* argv[])
{
    int x = (int) argv;
    int ret = 0;
    int h;

    ret = factorialNotOK(x);
    printf("The factorial of 10 is: %d\n", ret);

    for(h = 0; h< 50; h++)
    {
        printf(" %d,", page_pid[h]);
    }
    printf("\n");

    return 1;
}

int ciclo(int argc, char* argv[])
{
    int *key;
    PROCESS *proc = GetProcessByPID(CurrentPID);

    _Sti();

    key = wMalloc(sizeof(int));
    *key = -1;
    
    while(*key != 113)
    {
        if(CurrentTTY == proc->tty)
            read(stdin, (void *)key, 1);

        printf(".");
    }

    printf("\n");

    return 1;
}

int hola(int argc, char* argv[])
{
    int n = 0;

    _Sti();
    while (n != 10000)
    {
        printf("Hola %d\n",n++);
    }

    return 1;
}

int counter(int argc, char* argv[])
{
    int h;
    int i = 5;
    int * p;

    while (i--)
    {
        printf("pid:");
        for(h = 0; h< 50; h++)
        {
            printf(" %d,", page_pid[h]);
        }
        printf("\n");
        p = wMalloc(3000);

        Sleep(1);
    }

    return 0;
}
